package com.lanou3g.code0418.wait;

/**
 * Created by dllo on 18/4/18.
 */

/*
    wait / sleep
 共同点:都会让线程停止
 区别:1. wait是object类的方法,任何对象都有
    sleep是Thread类的方法
 2. wait 必须写在synchronized代码快中,必须是锁对象调用
    sleep没有限制,不需要synchronized
 3,wait会释放锁对象
    sleep不会释放锁
 4.wait可以被别的线程唤醒
    sleep需要等待时间结束
  */

public class Main {
    public static void main(String[] args) {
        final Object lock = new Object();

        Thread a = new Thread() {
            @Override
            public void run() {
                System.out.println("a线程开始了");
                synchronized (lock) {
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("a线程结束了");
            }
        };
        a.start();

        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("准备唤醒a线程");
        synchronized (lock) {
            lock.notify();
        }
    }
}
